package com.petsdar.service;

import com.petsdar.dao.EmpMapper;
import com.petsdar.dao.ShopMapper;
import com.petsdar.pojo.Shop;
import com.petsdar.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/3
 * Time: 10:42
 * Description: 对店铺相关业务逻辑处理接口的实现
 * Version: V1.0
 */

@Service
public class ShopServiceImpl implements ShopService{
    /** shop相关数据持久化实例对象 */
    private ShopMapper shopMapper;
    private EmpMapper empMapper;

    @Autowired
    public void setShopMapper(ShopMapper shopMapper) {
        this.shopMapper = shopMapper;
    }

    @Autowired
    public void setEmpMapper(EmpMapper empMapper) {
        this.empMapper = empMapper;
    }

    /**
     * 获取所有店铺信息
     * @return
     */
    @Override
    public List<Shop> getShop() {
        List<Shop> shops = shopMapper.getShop();
        return shops;
    }

    /**
     * 获取所有店铺名称
     * @return
     */
    @Override
    public List<Shop> getShopInfo() {
        List<Shop> shopInfo = shopMapper.getShopInfo();
        return shopInfo;
    }

    /**
     * 添加新店铺信息。解析shopJson为shop对象，根据当前系统时间生成ID，
     * 创建shop的数据创建时间、更新时间。完善shop对象后进行持久化操作。
     * @param shopJson 店铺信息相关json数据
     * @return
     */
    @Override
    public boolean addShop(String shopJson) {
        if (!shopJson.isEmpty()){
            Shop shop = JsonUtil.toObject(shopJson, Shop.class, this.getClass());
            System.out.println(shop);
            Date date = new Date();
            Random random = new Random();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddss");
            shop.setShopId(new BigInteger(format.format(date)+((random.nextInt(90)+10))));
            shop.setSpCreateTime(date);
            shop.setSpUpdateTime(date);
            int count = shopMapper.addShop(shop);
            if (count == 1){
                return true;
            }
        }
        return false;
    }

    /**
     * 通过店铺id删除店铺信息。需要先查看是否有员工存在该店铺。
     * 有，则不能被删除。没有，则可以被删除。
     * @param shopId 店铺id
     * @return
     */
    @Override
    public boolean delShop(String shopId) {
        if (!shopId.isEmpty()){
            String shopName = shopMapper.getShopName(shopId);
            String empCount = empMapper.getEmpCount(shopName);
            if (empCount != null){
                return false;
            }else {
                int count = shopMapper.delShop(shopId);
                if (count == 1){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 更新店铺信息。解析shopJson为shop对象，设置shop数据的更新时间。
     * 完善shop对象后，对其持久操作。
     * @param shopJson 店铺信息相关json数据
     * @return
     */
    @Override
    public boolean updateShop(String shopJson) {
        if (!shopJson.isEmpty()){
            Shop shop = JsonUtil.toObject(shopJson, Shop.class, this.getClass());
            shop.setSpUpdateTime(new Date());
            String shopName = shopMapper.getShopName(shop.getShopId().toString());
            if (shopName != shop.getChain()){
                empMapper.updateEmpShop(shop.getChain(), shopName);
            }
            int count = shopMapper.updateShop(shop);
            if (count == 1){
                return true;
            }
        }
        return false;
    }
}
